﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class mobile_lottery : System.Web.UI.Page
{
    public string enter_id;
    public string ind_id;
    public string lid;
    protected void Page_Load(object sender, EventArgs e)
    {
        enter_id = this.Request.Params["eid"];
        ind_id = this.Request.Params["indid"];
        lid = this.Request.Params["lid"];
        //TO-DO: 获取的是抽奖信息的各个项目，包括标题，开始 结束时间，规则，三个等级的奖品名称和数量，都在sql语句中已经获得，直接把数据显示到aspx就可以了
        //----cyh 2013/9/15 15:51
        string title = "";
        DateTime start = new DateTime();
        DateTime end = new DateTime();
        string rules = "";
        string fname = "";
        string sname = "";
        string tname = "";//NAMES OF THREE CLASSES OF AWARD
        int fnum = 0;
        int snum = 0;
        int tnum = 0;//NUMBER OF THE THREE AWARD
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand get_info=new SqlCommand();
        get_info.Connection=sc;
        get_info.CommandText = "select INFO_LOTTERY.LOTTERY_TITLE,INFO_LOTTERY.LOTTERY_START,INFO_LOTTERY.LOTTERY_END,INFO_LOTTERY.RULES,a.PRODUCT_NAME as 'FPRIZE',b.PRODUCT_NAME as 'SPRIZE',c.PRODUCT_NAME as 'TPRIZE',INFO_LOTTERY.FPRIZE_NUM,INFO_LOTTERY.SPRIZE_NUM,INFO_LOTTERY.TPRIZE_NUM from INFO_PRODUCT as a,INFO_PRODUCT as b,INFO_PRODUCT as c,INFO_LOTTERY where INFO_LOTTERY.LOTTERY_ID=@id1 and INFO_LOTTERY.FPRIZE_ID=a.PRODUCT_ID and INFO_LOTTERY.SPRIZE_ID=b.PRODUCT_ID and INFO_LOTTERY.TPRIZE_ID=c.PRODUCT_ID";
        get_info.Parameters.AddWithValue("@id1",lid);
        SqlDataReader r = get_info.ExecuteReader();
        while (r.Read())
        {
            title = r.GetString(r.GetOrdinal("LOTTERY_TITLE"));
            start = r.GetDateTime(r.GetOrdinal("LOTTERY_START"));
            end = r.GetDateTime(r.GetOrdinal("LOTTERY_END"));
            rules = r.GetString(r.GetOrdinal("RULES"));
            fname = r.GetString(r.GetOrdinal("FPRIZE"));
            sname = r.GetString(r.GetOrdinal("SPRIZE"));
            tname = r.GetString(r.GetOrdinal("TPRIZE"));
            fnum = r.GetInt32(r.GetOrdinal("FPRIZE_NUM"));
            snum = r.GetInt32(r.GetOrdinal("SPRIZE_NUM"));
            tnum = r.GetInt32(r.GetOrdinal("TPRIZE_NUM"));
            
        }
        r.Close();
        de.Text = "开始时间："+start+"<br />结束时间:"+end+"<br /><p>抽奖规则:"+rules+"</p><br />一等奖："+fname+"数量："+fnum
            +"<br />二等奖："+sname+"数量："+snum+"<br />三等奖"+tname+"数量："+tnum+"<br />";
        //TO-DO:在此处添加你的代码
        /*
        SqlCommand check_attend = new SqlCommand("select IND_ID from LOTTERY_ATTEND where IND_ID=@iid and LOTTERY_ID=@lid",sc);
        check_attend.Parameters.AddWithValue("@iid",ind_id);
        check_attend.Parameters.AddWithValue("@lid",lid);
        object a = check_attend.ExecuteScalar();
        if (a!=null)
        {
            Response.Write("<script>alert('您已经参加过此次抽奖。')</script>");
            Response.Write("<script>window.opener = null;window.open('','_self');window.close();</script>");
        }*/
    }
    protected void getprise_Click(object sender, EventArgs e)
    {
       string prise = prise2.Text;
        int fnum = 0;
        int snum = 0;
        int tnum = 0;
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand getnum = new SqlCommand("select FPRIZE_NUM,SPRIZE_NUM,TPRIZE_NUM from INFO_LOTTERY where LOTTERY_ID=@lid1",sc);
        getnum.Parameters.AddWithValue("@lid1",lid);
        SqlDataReader r = getnum.ExecuteReader();
        while (r.Read())
        {
            fnum = r.GetInt32(r.GetOrdinal("FPRIZE_NUM"));
            snum = r.GetInt32(r.GetOrdinal("SPRIZE_NUM"));
            tnum = r.GetInt32(r.GetOrdinal("TPRIZE_NUM"));
        }
        r.Close();

        if (prise.Equals("一等奖"))
        {
            if (fnum == 0)
            {
                prise = "二等奖";
            }
            else
            {
                //一等奖数量-1,然后在lottery_attend中录入信息
                SqlCommand update_fnum = new SqlCommand("update INFO_LOTTERY set FPRIZE_NUM=FPRIZE_NUM-1 where LOTTERY_ID=@lid2",sc);
                update_fnum.Parameters.AddWithValue("@lid2", lid);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_fnum.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                SqlCommand update_attend1 = new SqlCommand("update LOTTERY_ATTEND set AWARD=@aw1", sc);
                update_attend1.Parameters.AddWithValue("@aw1",1);
                 while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_attend1.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                Response.Redirect("mobile_yeslottery.aspx");
                return;

            }
        }
        if (prise.Equals("二等奖"))
        {
            if (snum == 0)
            {
                prise = "三等奖";
            }
            else
            {
                //二等奖数量-1,然后在lottery_attend中录入信息
                SqlCommand update_snum = new SqlCommand("update INFO_LOTTERY set SPRIZE_NUM=SPRIZE_NUM-1 where LOTTERY_ID=@lid4", sc);
                update_snum.Parameters.AddWithValue("@lid4", lid);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_snum.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                SqlCommand update_attend2 = new SqlCommand("update LOTTERY_ATTEND set AWARD=@aw2", sc);
                update_attend2.Parameters.AddWithValue("@aw2", 2);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_attend2.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                Response.Redirect("mobile_yeslottery.aspx");
                return;
            }
        }
        else
        {
            if (tnum == 0)
            {
                prise = "";
                Page.RegisterStartupScript("ss", "<script>alert('对不起，奖项已经领完，活动已结束!')</script>");
                return;
            }
            else
            {
                //三等奖数量-1,然后在lottery_attend中录入信息
                SqlCommand update_tnum = new SqlCommand("update INFO_LOTTERY set TPRIZE_NUM=TPRIZE_NUM-1 where LOTTERY_ID=@lid6", sc);
                update_tnum.Parameters.AddWithValue("@lid6", lid);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_tnum.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                SqlCommand update_attend3 = new SqlCommand("update LOTTERY_ATTEND set AWARD=@aw3", sc);
                update_attend3.Parameters.AddWithValue("@aw3", 3);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁   
                try
                {
                    update_attend3.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";
                }
                Response.Redirect("mobile_yeslottery.aspx");
                return;
            }
        }
    }
 
    public string cost()
    {
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
           SqlCommand check_attend = new SqlCommand("select IND_ID from LOTTERY_ATTEND where IND_ID=@iid and LOTTERY_ID=@lid",sc);
        check_attend.Parameters.AddWithValue("@iid",ind_id);
        check_attend.Parameters.AddWithValue("@lid",lid);
        object a = check_attend.ExecuteScalar();
        if (a != null)
        {
            Page.RegisterStartupScript("ss", "<script>alert('您已经参加过此次抽奖!')</script>");
            Response.Redirect("http://lcsy2008.eicp.net/wbkpro/mobile_nolottery.aspx");
           
        }
        else
        {
            SqlCommand insert_attend2 = new SqlCommand("insert into LOTTERY_ATTEND values (@iid2,@lid5,@adate2,@aw2)", sc);
            insert_attend2.Parameters.AddWithValue("@iid2", ind_id);
            insert_attend2.Parameters.AddWithValue("@lid5", lid);
            insert_attend2.Parameters.AddWithValue("@adate2", DateTime.Now);
            insert_attend2.Parameters.AddWithValue("@aw2", 0);
            while (Application["sqllock"] == "lock")
            {//检测是否已经锁住
                System.Threading.Thread.Sleep(2000);
            }
            Application["sqllock"] = "lock";//上锁   
            try
            {
                insert_attend2.ExecuteNonQuery();
            }
            finally
            {
                Application["sqllock"] = "unlock";
            }
        }
        return "";
    }
}
